/**
 * Created by Administrator on 2017/4/20.
 */

function PublicCards(room) {
    this.room = room;

    this.cards = [];         // 牌
    this.getIndex = 0;      // 取牌位置
}

PublicCards.prototype = {

    // 生成牌组
    gen: function () {
        // 初始化数据
        var subConfig = this.room.getSubConfig();
        var colors = subConfig.colors;

        this.cards = [];
        this.getIndex = 0;

        // 生成牌
        var originCards = [
            20, 20,
            21, 21,
            22, 22,
            23, 23,
            24, 24,
            26, 26,
            27, 27,
            28, 28,
            29, 29,
            30, 30,
            31, 31,
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10
        ];

        while (originCards.length > 0) {
            var iRamdom = Math.floor(Math.random() * originCards.length);
            this.cards.push(originCards[iRamdom]);
            originCards.splice(iRamdom, 1);
        }
    },

    // 看下一张牌而不摸牌
    lookCard: function () {
        return (this.getIndex >= this.cards.length) ? 0 : this.cards[this.getIndex];
    },

    // 摸牌
    getCard: function () {
        var card = this.cards[this.getIndex];
        this.getIndex += 1;
        return card;
    },

    // 摸牌
    getCards: function (num, preCards) {
        var rtnCards = [];
        for (var i = 0; i < num; ++i) {
            if (preCards && i < preCards.length) {
                var preCard = preCards[i];
                for (var ci = this.getIndex, cl = this.cards.length; ci < cl; ++ci) {
                    if (this.cards[ci] == preCard) {
                        this.cards[ci] = this.cards[this.getIndex];
                        this.cards[this.getIndex] = preCard;
                        break;
                    }
                }
            }

            rtnCards.push(this.cards[this.getIndex]);
            ++this.getIndex;
        }
        return rtnCards;
    },

    // 公牌是否已经摸完
    isEmpty: function () {
        return this.getIndex >= this.cards.length;
    },

    // 获取剩余牌数量
    getRemainNum: function () {
        return this.cards.length - this.getIndex;
    },

    // 获取剩余公牌
    getRemainCards: function () {
        return this.cards.slice(this.getIndex);
    },

    // setRemainCards: function (newCardsArray) {
    //     if (newCardsArray.length != this.getRemainNum()) {
    //         return;
    //     }

    //     var remainCards = Poker.buildFromArray(this.getRemainCards());
    //     var newCards = Poker.buildFromArray(newCardsArray);
    //     if (!Poker.isSameCards(remainCards, newCards)) {
    //         return;
    //     }

    //     for (var i = 0, l = newCardsArray.length; i < l; ++i) {
    //         this.cards[this.getIndex + i] = newCardsArray[i];
    //     }
    // },
    getPlayedCards:function(){
        return this.cards.slice(0,this.getIndex);
    }
};
exports.PublicCards = PublicCards;